package com.teacher.system.controller;

import com.teacher.system.common.Result;
import com.teacher.system.entity.Teacher;
import com.teacher.system.service.TeacherService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    /**
     * 新增教师
     * @param teacher
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return Result.success();
    }

    /**
     *根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result update(@PathVariable Long id) {
        teacherService.del(id);
        return Result.success();
    }

    /**
     * 更新教师
     * @param teacher
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return Result.success();
    }

    /**
     * 根据id查询教师
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Teacher> findById(@PathVariable Long id) {
        return Result.success(teacherService.findById(id));
    }

    @GetMapping
    public Result<List<Teacher>> findAll() {
        return Result.success(teacherService.findAll());
    }

    /**
     * 分页模糊查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Teacher>> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          @RequestParam(required = false) String name) {
        return Result.success(teacherService.findPage(pageNum,pageSize,name));
    }

//    public Result<Page<Teacher>> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
//                                          @RequestParam(defaultValue = "10") Integer pageSize,
//                                          @RequestParam(required = false) String name) {
//        return Result.success(teacherService.findPage(pageNum,pageSize,name));
//    }
}













